<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ODF结构管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        :root {
            --primary: #2c6fbb;
            --secondary: #3a9d23;
            --accent: #ffc107;
            --dark: #1e2a38;
            --light: #f5f7fa;
            --success: #4caf50;
            --warning: #ff9800;
            --danger: #f44336;
            --info: #2196f3;
            --grey: #e0e0e0;
        }

        body {
            background: linear-gradient(135deg, #1e3c72, #2a5298);
            min-height: 100vh;
            color: #333;
            padding: 20px;
        }

        .app-container {
            max-width: 1400px;
            margin: 0 auto;
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
            overflow: hidden;
        }

        .app-header {
            background-color: var(--dark);
            color: white;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 20px;
        }

        .app-header h1 {
            display: flex;
            align-items: center;
            gap: 12px;
            font-weight: 600;
            font-size: 1.8rem;
        }

        .app-header h1 i {
            color: var(--accent);
        }

        .mode-switch {
            display: flex;
            gap: 10px;
            background-color: rgba(255, 255, 255, 0.15);
            border-radius: 50px;
            padding: 5px;
        }

        .mode-switch button {
            border: none;
            background: none;
            color: rgba(255, 255, 255, 0.7);
            padding: 10px 20px;
            border-radius: 50px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
        }

        .mode-switch button.active {
            background-color: var(--accent);
            color: var(--dark);
        }

        .tab-nav {
            display: flex;
            background-color: var(--primary);
            padding: 0 20px;
        }

        .tab-nav button {
            background: none;
            border: none;
            color: rgba(255, 255, 255, 0.8);
            padding: 16px 24px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 8px;
            position: relative;
            transition: all 0.3s;
        }

        .tab-nav button.active {
            color: white;
            background-color: rgba(255, 255, 255, 0.15);
        }

        .tab-nav button.active::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background-color: var(--accent);
        }

        .app-content {
            padding: 25px;
            min-height: 500px;
        }

        .odf-management {
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        .toolbar {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            gap: 20px;
            background-color: #f0f5ff;
            padding: 20px;
            border-radius: 10px;
            border: 1px solid #d0e0ff;
        }

        .odf-filter, .search-box, .actions {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        select, input {
            padding: 12px 16px;
            border: 1px solid #d0d0d0;
            border-radius: 8px;
            font-size: 1rem;
        }

        .search-box {
            position: relative;
        }

        .search-box i {
            position: absolute;
            right: 15px;
            color: #888;
        }

        .search-box input {
            padding-right: 40px;
        }

        .actions button {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 12px 20px;
            border-radius: 8px;
            border: none;
            background-color: var(--primary);
            color: white;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .actions button:hover {
            background-color: #1a519e;
        }

        .btn-template {
            background-color: #6c757d !important;
        }

        .btn-template:hover {
            background-color: #5a6268 !important;
        }

        .btn-validate {
            background-color: var(--success) !important;
        }

        .btn-validate:hover {
            background-color: #3d8b40 !important;
        }

        .odf-structure-container {
            display: flex;
            gap: 25px;
            flex-wrap: wrap;
        }

        .odf-list {
            flex: 1;
            min-width: 300px;
            background-color: #f8f9fc;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            border: 1px solid #e0e0e0;
            max-height: 600px;
            overflow-y: auto;
        }

        .odf-detail {
            flex: 2;
            min-width: 500px;
            background-color: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            border: 1px solid #e0e0e0;
        }

        .odf-item {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 10px;
            background-color: white;
            border: 1px solid #e2e8f0;
            cursor: pointer;
            transition: all 0.3s;
        }

        .odf-item:hover {
            background-color: #f0f5ff;
            border-color: var(--primary);
        }

        .odf-item.active {
            background-color: #e3f2fd;
            border-color: var(--primary);
        }

        .odf-item h3 {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 8px;
            color: var(--primary);
        }

        .odf-item .location {
            font-size: 0.9rem;
            color: #666;
            margin-bottom: 5px;
        }

        .odf-item .stats {
            display: flex;
            gap: 15px;
            font-size: 0.85rem;
            color: #666;
        }

        .odf-detail-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f3f7;
        }

        .odf-detail-header h2 {
            color: var(--primary);
        }

        .odf-form {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 25px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #444;
        }

        .form-group input, .form-group select {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 1rem;
        }

        .btn-save {
            background-color: var(--success);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 8px;
            font-weight: 500;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 8px;
            margin-top: 10px;
        }

        .odm-container {
            margin-top: 25px;
        }

        .odm-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
            margin-top: 15px;
        }

        .odm-card {
            background-color: #f8f9fc;
            border: 1px solid #e2e8f0;
            border-radius: 10px;
            padding: 15px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
        }

        .odm-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            border-color: var(--primary);
        }

        .odm-card.active {
            background-color: #e3f2fd;
            border-color: var(--primary);
        }

        .odm-card h3 {
            color: var(--primary);
            margin-bottom: 10px;
        }

        .tray-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }

        .tray-card {
            background-color: white;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 15px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
        }

        .tray-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
            border-color: var(--accent);
        }

        .tray-card.active {
            background-color: #fff8e1;
            border-color: var(--accent);
        }

        .tray-card h4 {
            color: #333;
            margin-bottom: 10px;
        }

        .port-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }

        .port-card {
            background-color: white;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 15px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
        }

        .port-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
            border-color: var(--success);
        }

        .port-card.active {
            background-color: #e8f5e9;
            border-color: var(--success);
        }

        .port-card h5 {
            color: #333;
            margin-bottom: 8px;
        }

        .port-status {
            font-size: 0.8rem;
            padding: 3px 8px;
            border-radius: 50px;
            display: inline-block;
        }

        .status-pending {
            background-color: #fff8e1;
            color: #ff8f00;
        }

        .status-success {
            background-color: #e8f5e9;
            color: #2e7d32;
        }

        .status-error {
            background-color: #ffebee;
            color: #c62828;
        }

        .port-detail-panel {
            background-color: white;
            border: 1px solid #e0e0e0;
            border-radius: 10px;
            padding: 25px;
            margin-top: 25px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
        }

        .detail-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f3f7;
        }

        .detail-content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 25px;
        }

        .detail-section {
            background-color: #f8f9fc;
            border-radius: 8px;
            padding: 20px;
            border: 1px solid #edf2f7;
        }

        .detail-section h3 {
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #e2e8f0;
            color: var(--primary);
        }

        .info-item {
            display: flex;
            margin-bottom: 10px;
        }

        .info-label {
            font-weight: 600;
            min-width: 140px;
            color: #4a5568;
        }

        .info-value {
            color: #2d3748;
        }

        .status-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 5px 12px;
            border-radius: 50px;
            font-size: 0.9rem;
        }

        .status-badge.pending {
            background-color: #fff8e1;
            color: #ff8f00;
        }

        .status-badge.success {
            background-color: #e8f5e9;
            color: #2e7d32;
        }

        .status-badge.error {
            background-color: #ffebee;
            color: #c62828;
        }

        .history-item {
            padding: 10px 0;
            border-bottom: 1px dashed #e2e8f0;
        }

        .history-item:last-child {
            border-bottom: none;
        }

        .btn-save {
            background-color: var(--success);
            color: white;
            border: none;
            border-radius: 6px;
            padding: 8px 12px;
            cursor: pointer;
        }

        /* 响应式调整 */
        @media (max-width: 992px) {
            .toolbar {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .detail-content {
                grid-template-columns: 1fr;
            }
            
            .odf-filter, .search-box {
                width: 100%;
            }
            
            .odf-form {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 768px) {
            .app-header {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .tab-nav {
                flex-direction: column;
                padding: 0;
            }
            
            .tab-nav button {
                width: 100%;
            }
            
            .odf-structure-container {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- 应用头部 -->
        <div class="app-header">
            <h1><i class="fas fa-project-diagram"></i>ODF结构管理系统</h1>
            <div class="mode-switch">
                <button class="active">单扫模式</button>
                <button>群扫模式</button>
            </div>
        </div>
        
        <!-- 标签页导航 -->
        <div class="tab-nav">
            <button class="active"><i class="fas fa-sitemap"></i> ODF结构管理</button>
            <button><i class="fas fa-tags"></i> 标签填报管理</button>
        </div>
        
        <!-- 主内容区 -->
        <div class="app-content">
            <div class="odf-management">
                <!-- 工具栏 -->
                <div class="toolbar">
                    <div class="odf-filter">
                        <label>机房筛选：</label>
                        <select id="room-filter">
                            <option>全部机房</option>
                            <option>核心机房</option>
                            <option>接入机房</option>
                            <option>汇聚机房</option>
                        </select>
                    </div>
                    
                    <div class="search-box">
                        <input type="text" placeholder="搜索ODF名称..." id="odf-search">
                        <i class="fas fa-search"></i>
                    </div>
                    
                    <div class="actions">
                        <button id="btn-new-odf"><i class="fas fa-plus-circle"></i> 新建ODF</button>
                        <button id="btn-import"><i class="fas fa-file-import"></i> 导入结构</button>
                        <button id="btn-export"><i class="fas fa-file-export"></i> 导出结构</button>
                    </div>
                </div>
                
                <!-- ODF结构管理 -->
                <div class="odf-structure-container">
                    <!-- ODF列表 -->
                    <div class="odf-list">
                        <h3>ODF列表</h3>
                        <div id="odf-list-container">
                            <!-- ODF项目将通过JS动态生成 -->
                        </div>
                    </div>
                    
                    <!-- ODF详情 -->
                    <div class="odf-detail">
                        <div class="odf-detail-header">
                            <h2 id="current-odf-name">新建ODF结构</h2>
                            <button class="btn-save" id="btn-save-odf"><i class="fas fa-save"></i> 保存结构</button>
                        </div>
                        
                        <!-- ODF基本信息表单 -->
                        <div class="odf-form">
                            <div class="form-group">
                                <label for="odf-name">ODF名称</label>
                                <input type="text" id="odf-name" placeholder="例如：XX机房-主配线架">
                            </div>
                            
                            <div class="form-group">
                                <label for="odf-location">位置信息</label>
                                <input type="text" id="odf-location" placeholder="例如：机柜A-03">
                            </div>
                            
                            <div class="form-group">
                                <label for="odf-type">ODF类型</label>
                                <select id="odf-type">
                                    <option>标准ODF</option>
                                    <option>高密度ODF</option>
                                    <option>智能ODF</option>
                                </select>
                            </div>
                            
                            <div class="form-group">
                                <label for="odm-count">ODM数量</label>
                                <select id="odm-count">
                                    <option>4</option>
                                    <option>8</option>
                                    <option selected>16</option>
                                    <option>24</option>
                                    <option>32</option>
                                </select>
                            </div>
                        </div>
                        
                        <!-- ODM管理 -->
                        <div class="odm-container">
                            <h3>ODM管理</h3>
                            <div class="odm-grid" id="odm-grid">
                                <!-- ODM卡片将通过JS动态生成 -->
                            </div>
                        </div>
                        
                        <!-- 托盘管理 -->
                        <div class="tray-container" id="tray-container" style="display: none;">
                            <h3>托盘管理 <span id="current-odm-name"></span></h3>
                            <div class="tray-grid" id="tray-grid">
                                <!-- 托盘卡片将通过JS动态生成 -->
                            </div>
                        </div>
                        
                        <!-- 端口管理 -->
                        <div class="port-container" id="port-container" style="display: none;">
                            <h3>端口管理 <span id="current-tray-name"></span></h3>
                            <div class="port-grid" id="port-grid">
                                <!-- 端口卡片将通过JS动态生成 -->
                            </div>
                        </div>
                        
                        <!-- 端口详情面板 -->
                        <div class="port-detail-panel" id="port-detail" style="display: none;">
                            <div class="detail-header">
                                <h2>端口详情 <span id="port-path"></span></h2>
                            </div>
                            
                            <div class="detail-content">
                                <div class="detail-section">
                                    <h3><i class="fas fa-qrcode"></i> 标签信息</h3>
                                    
                                    <div class="info-item">
                                        <div class="info-label">电子标签值：</div>
                                        <div class="info-value" id="port-tag-value">未设置</div>
                                    </div>
                                    
                                    <div class="info-item">
                                        <div class="info-label">插头标签值：</div>
                                        <div class="info-value" id="plug-tag-value">未设置</div>
                                    </div>
                                    
                                    <div class="info-item">
                                        <div class="info-label">当前状态：</div>
                                        <div class="info-value">
                                            <span class="status-badge pending" id="port-status"><i class="fas fa-clock"></i> 待扫描</span>
                                        </div>
                                    </div>
                                    
                                    <div class="info-item">
                                        <div class="info-label">最后扫描时间：</div>
                                        <div class="info-value" id="last-scan-time">未扫描</div>
                                    </div>
                                </div>
                                
                                <div class="detail-section">
                                    <h3><i class="fas fa-history"></i> 操作</h3>
                                    
                                    <div class="info-item">
                                        <button class="btn-scan" id="btn-scan-port"><i class="fas fa-qrcode"></i> 扫描标签</button>
                                        <button class="btn-scan" id="btn-edit-tags" style="background-color: var(--primary);"><i class="fas fa-edit"></i> 编辑标签</button>
                                    </div>
                                    
                                    <div class="info-item">
                                        <button class="btn-scan" id="btn-set-default" style="background-color: var(--warning);"><i class="fas fa-magic"></i> 设为默认值</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // ODF数据结构
        const odfSystem = {
            odfs: [],
            currentODF: null,
            currentODM: null,
            currentTray: null,
            currentPort: null
        };

        // 初始化示例数据
        function initSampleData() {
            // 创建示例ODF
            const odf1 = {
                id: 1,
                name: "核心机房-主配线架",
                location: "机柜A-03",
                type: "标准ODF",
                odmCount: 16,
                odms: []
            };

            // 添加ODM
            for (let i = 1; i <= odf1.odmCount; i++) {
                const odm = {
                    id: i,
                    trays: []
                };

                // 添加托盘
                for (let j = 1; j <= 12; j++) {
                    const tray = {
                        id: j,
                        ports: []
                    };

                    // 添加端口
                    for (let k = 1; k <= 12; k++) {
                        tray.ports.push({
                            id: k,
                            tagValue: "",
                            plugValue: "",
                            status: "pending",
                            lastScan: null
                        });
                    }

                    odm.trays.push(tray);
                }

                odf1.odms.push(odm);
            }

            odfSystem.odfs.push(odf1);
            odfSystem.currentODF = odf1;
        }

        // 渲染ODF列表
        function renderODFList() {
            const container = document.getElementById('odf-list-container');
            container.innerHTML = '';
            
            odfSystem.odfs.forEach(odf => {
                const isActive = odfSystem.currentODF && odfSystem.currentODF.id === odf.id;
                const odmCount = odf.odms.length;
                const portCount = odmCount * 12 * 12;
                
                const odfItem = document.createElement('div');
                odfItem.className = `odf-item ${isActive ? 'active' : ''}`;
                odfItem.dataset.id = odf.id;
                
                odfItem.innerHTML = `
                    <h3><i class="fas fa-server"></i> ${odf.name}</h3>
                    <div class="location">${odf.location}</div>
                    <div class="stats">
                        <span><i class="fas fa-cube"></i> ${odmCount}个ODM</span>
                        <span><i class="fas fa-plug"></i> ${portCount}个端口</span>
                    </div>
                `;
                
                odfItem.addEventListener('click', () => {
                    selectODF(odf);
                });
                
                container.appendChild(odfItem);
            });
            
            // 添加新建按钮
            const newItem = document.createElement('div');
            newItem.className = 'odf-item';
            newItem.innerHTML = `
                <h3><i class="fas fa-plus-circle"></i> 新建ODF结构</h3>
                <div class="location">点击创建新的ODF</div>
            `;
            newItem.addEventListener('click', createNewODF);
            container.appendChild(newItem);
        }

        // 选择ODF
        function selectODF(odf) {
            odfSystem.currentODF = odf;
            odfSystem.currentODM = null;
            odfSystem.currentTray = null;
            odfSystem.currentPort = null;
            
            // 更新UI
            document.getElementById('current-odf-name').textContent = odf.name;
            document.getElementById('odf-name').value = odf.name;
            document.getElementById('odf-location').value = odf.location;
            document.getElementById('odf-type').value = odf.type;
            document.getElementById('odm-count').value = odf.odmCount;
            
            // 渲染ODM网格
            renderODMGrid();
            
            // 隐藏托盘和端口视图
            document.getElementById('tray-container').style.display = 'none';
            document.getElementById('port-container').style.display = 'none';
            document.getElementById('port-detail').style.display = 'none';
            
            // 更新列表选中状态
            document.querySelectorAll('.odf-item').forEach(item => {
                item.classList.remove('active');
                if (parseInt(item.dataset.id) === odf.id) {
                    item.classList.add('active');
                }
            });
        }

        // 创建新ODF
        function createNewODF() {
            const newODF = {
                id: Date.now(), // 使用时间戳作为ID
                name: "新ODF结构",
                location: "",
                type: "标准ODF",
                odmCount: 16,
                odms: []
            };
            
            // 初始化ODM
            for (let i = 1; i <= newODF.odmCount; i++) {
                const odm = {
                    id: i,
                    trays: []
                };
                
                // 初始化托盘
                for (let j = 1; j <= 12; j++) {
                    const tray = {
                        id: j,
                        ports: []
                    };
                    
                    // 初始化端口
                    for (let k = 1; k <= 12; k++) {
                        tray.ports.push({
                            id: k,
                            tagValue: "",
                            plugValue: "",
                            status: "pending",
                            lastScan: null
                        });
                    }
                    
                    odm.trays.push(tray);
                }
                
                newODF.odms.push(odm);
            }
            
            odfSystem.odfs.push(newODF);
            selectODF(newODF);
            renderODFList();
        }

        // 渲染ODM网格
        function renderODMGrid() {
            const grid = document.getElementById('odm-grid');
            grid.innerHTML = '';
            
            if (!odfSystem.currentODF) return;
            
            odfSystem.currentODF.odms.forEach(odm => {
                const isActive = odfSystem.currentODM && odfSystem.currentODM.id === odm.id;
                
                const odmCard = document.createElement('div');
                odmCard.className = `odm-card ${isActive ? 'active' : ''}`;
                odmCard.dataset.id = odm.id;
                odmCard.innerHTML = `
                    <h3>ODM-${odm.id}</h3>
                    <div>12个托盘</div>
                    <div>144个端口</div>
                `;
                
                odmCard.addEventListener('click', () => {
                    selectODM(odm);
                });
                
                grid.appendChild(odmCard);
            });
        }

        // 选择ODM
        function selectODM(odm) {
            odfSystem.currentODM = odm;
            odfSystem.currentTray = null;
            odfSystem.currentPort = null;
            
            // 更新UI
            document.getElementById('current-odm-name').textContent = `(ODM-${odm.id})`;
            document.getElementById('tray-container').style.display = 'block';
            document.getElementById('port-container').style.display = 'none';
            document.getElementById('port-detail').style.display = 'none';
            
            // 渲染托盘网格
            renderTrayGrid();
            
            // 更新ODM选中状态
            document.querySelectorAll('.odm-card').forEach(card => {
                card.classList.remove('active');
                if (parseInt(card.dataset.id) === odm.id) {
                    card.classList.add('active');
                }
            });
        }

        // 渲染托盘网格
        function renderTrayGrid() {
            const grid = document.getElementById('tray-grid');
            grid.innerHTML = '';
            
            if (!odfSystem.currentODM) return;
            
            odfSystem.currentODM.trays.forEach(tray => {
                const isActive = odfSystem.currentTray && odfSystem.currentTray.id === tray.id;
                
                const trayCard = document.createElement('div');
                trayCard.className = `tray-card ${isActive ? 'active' : ''}`;
                trayCard.dataset.id = tray.id;
                trayCard.innerHTML = `
                    <h4>托盘-${tray.id}</h4>
                    <div>12个端口</div>
                `;
                
                trayCard.addEventListener('click', () => {
                    selectTray(tray);
                });
                
                grid.appendChild(trayCard);
            });
        }

        // 选择托盘
        function selectTray(tray) {
            odfSystem.currentTray = tray;
            odfSystem.currentPort = null;
            
            // 更新UI
            document.getElementById('current-tray-name').textContent = `(托盘-${tray.id})`;
            document.getElementById('port-container').style.display = 'block';
            document.getElementById('port-detail').style.display = 'none';
            
            // 渲染端口网格
            renderPortGrid();
            
            // 更新托盘选中状态
            document.querySelectorAll('.tray-card').forEach(card => {
                card.classList.remove('active');
                if (parseInt(card.dataset.id) === tray.id) {
                    card.classList.add('active');
                }
            });
        }

        // 渲染端口网格
        function renderPortGrid() {
            const grid = document.getElementById('port-grid');
            grid.innerHTML = '';
            
            if (!odfSystem.currentTray) return;
            
            odfSystem.currentTray.ports.forEach(port => {
                const isActive = odfSystem.currentPort && odfSystem.currentPort.id === port.id;
                
                const portCard = document.createElement('div');
                portCard.className = `port-card ${isActive ? 'active' : ''}`;
                portCard.dataset.id = port.id;
                
                let statusClass = 'status-pending';
                let statusText = '待扫描';
                
                if (port.status === 'success') {
                    statusClass = 'status-success';
                    statusText = '已验证';
                } else if (port.status === 'error') {
                    statusClass = 'status-error';
                    statusText = '不匹配';
                }
                
                portCard.innerHTML = `
                    <h5>端口-${port.id}</h5>
                    <div class="port-status ${statusClass}">${statusText}</div>
                `;
                
                portCard.addEventListener('click', () => {
                    selectPort(port);
                });
                
                grid.appendChild(portCard);
            });
        }

        // 选择端口
        function selectPort(port) {
            odfSystem.currentPort = port;
            
            // 更新UI
            const odmId = odfSystem.currentODM.id;
            const trayId = odfSystem.currentTray.id;
            const portId = port.id;
            
            document.getElementById('port-path').textContent = `ODM-${odmId} > 托盘-${trayId} > 端口-${portId}`;
            document.getElementById('port-tag-value').textContent = port.tagValue || "未设置";
            document.getElementById('plug-tag-value').textContent = port.plugValue || "未设置";
            
            // 更新状态
            const statusElement = document.getElementById('port-status');
            statusElement.className = 'status-badge ' + port.status;
            statusElement.innerHTML = port.status === 'pending' ? 
                '<i class="fas fa-clock"></i> 待扫描' : 
                port.status === 'success' ? 
                '<i class="fas fa-check-circle"></i> 已验证' : 
                '<i class="fas fa-times-circle"></i> 不匹配';
            
            // 更新扫描时间
            document.getElementById('last-scan-time').textContent = 
                port.lastScan ? new Date(port.lastScan).toLocaleString() : "未扫描";
            
            // 显示详情面板
            document.getElementById('port-detail').style.display = 'block';
            
            // 更新端口选中状态
            document.querySelectorAll('.port-card').forEach(card => {
                card.classList.remove('active');
                if (parseInt(card.dataset.id) === port.id) {
                    card.classList.add('active');
                }
            });
        }

        // 保存ODF结构
        function saveODF() {
            if (!odfSystem.currentODF) return;
            
            // 更新当前ODF数据
            odfSystem.currentODF.name = document.getElementById('odf-name').value;
            odfSystem.currentODF.location = document.getElementById('odf-location').value;
            odfSystem.currentODF.type = document.getElementById('odf-type').value;
            odfSystem.currentODF.odmCount = parseInt(document.getElementById('odm-count').value);
            
            // 更新UI
            document.getElementById('current-odf-name').textContent = odfSystem.currentODF.name;
            
            // 更新列表
            renderODFList();
            
            alert('ODF结构已保存！');
        }

        // 扫描端口标签
        function scanPort() {
            if (!odfSystem.currentPort) return;
            
            // 模拟扫描过程
            const scanTime = new Date();
            
            // 生成随机标签值（实际应用中应通过扫描设备获取）
            const tagValue = 'E200' + Math.floor(Math.random() * 1000000000000).toString(16).toUpperCase();
            const plugValue = 'E200' + Math.floor(Math.random() * 1000000000000).toString(16).toUpperCase();
            
            // 更新端口数据
            odfSystem.currentPort.tagValue = tagValue;
            odfSystem.currentPort.plugValue = plugValue;
            odfSystem.currentPort.status = 'success';
            odfSystem.currentPort.lastScan = scanTime;
            
            // 更新UI
            document.getElementById('port-tag-value').textContent = tagValue;
            document.getElementById('plug-tag-value').textContent = plugValue;
            
            const statusElement = document.getElementById('port-status');
            statusElement.className = 'status-badge success';
            statusElement.innerHTML = '<i class="fas fa-check-circle"></i> 已验证';
            
            document.getElementById('last-scan-time').textContent = scanTime.toLocaleString();
            
            // 更新端口网格
            renderPortGrid();
        }

        // 编辑标签
        function editTags() {
            if (!odfSystem.currentPort) return;
            
            const tagValue = prompt("请输入电子标签值：", odfSystem.currentPort.tagValue || "");
            if (tagValue === null) return;
            
            const plugValue = prompt("请输入插头标签值：", odfSystem.currentPort.plugValue || "");
            if (plugValue === null) return;
            
            // 更新端口数据
            odfSystem.currentPort.tagValue = tagValue;
            odfSystem.currentPort.plugValue = plugValue;
            odfSystem.currentPort.status = tagValue && plugValue ? 'success' : 'pending';
            
            // 更新UI
            document.getElementById('port-tag-value').textContent = tagValue || "未设置";
            document.getElementById('plug-tag-value').textContent = plugValue || "未设置";
            
            const statusElement = document.getElementById('port-status');
            statusElement.className = 'status-badge ' + odfSystem.currentPort.status;
            statusElement.innerHTML = odfSystem.currentPort.status === 'pending' ? 
                '<i class="fas fa-clock"></i> 待扫描' : 
                '<i class="fas fa-check-circle"></i> 已验证';
            
            // 更新端口网格
            renderPortGrid();
        }

        // 设置默认标签值
        function setDefaultTags() {
            if (!odfSystem.currentPort) return;
            
            // 生成默认标签值
            const odmId = odfSystem.currentODM.id.toString().padStart(2, '0');
            const trayId = odfSystem.currentTray.id.toString().padStart(2, '0');
            const portId = odfSystem.currentPort.id.toString().padStart(2, '0');
            
            const tagValue = `E200${odmId}${trayId}${portId}00000001`;
            const plugValue = `E200${odmId}${trayId}${portId}00000002`;
            
            // 更新端口数据
            odfSystem.currentPort.tagValue = tagValue;
            odfSystem.currentPort.plugValue = plugValue;
            odfSystem.currentPort.status = 'success';
            
            // 更新UI
            document.getElementById('port-tag-value').textContent = tagValue;
            document.getElementById('plug-tag-value').textContent = plugValue;
            
            const statusElement = document.getElementById('port-status');
            statusElement.className = 'status-badge success';
            statusElement.innerHTML = '<i class="fas fa-check-circle"></i> 已验证';
            
            // 更新端口网格
            renderPortGrid();
        }

        // 初始化
        document.addEventListener('DOMContentLoaded', () => {
            // 初始化示例数据
            initSampleData();
            
            // 渲染初始界面
            renderODFList();
            renderODMGrid();
            
            // 绑定事件
            document.getElementById('btn-save-odf').addEventListener('click', saveODF);
            document.getElementById('btn-new-odf').addEventListener('click', createNewODF);
            document.getElementById('btn-scan-port').addEventListener('click', scanPort);
            document.getElementById('btn-edit-tags').addEventListener('click', editTags);
            document.getElementById('btn-set-default').addEventListener('click', setDefaultTags);
            
            // 默认选择第一个ODF
            if (odfSystem.odfs.length > 0) {
                selectODF(odfSystem.odfs[0]);
            }
        });
    </script>
</body>
</html>